package com.example.fresh_fruit.service.Impl;

import com.example.fresh_fruit.dto.OrderDto;
import com.example.fresh_fruit.entity.Commodity;
import com.example.fresh_fruit.entity.OrderDetails;
import com.example.fresh_fruit.mapper.CommodityMapper;
import com.example.fresh_fruit.mapper.OrdersDetailsMapper;
import com.example.fresh_fruit.service.OrderDetailsService;
import com.example.fresh_fruit.service.OrderDtoService;
import com.example.fresh_fruit.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author :飞天の鱼
 * @date : 2022/8/22 17:44
 */
@Slf4j
@Service
public class OrderDetailsServiceImpl implements OrderDetailsService {

    @Autowired
    private OrdersDetailsMapper ordersDetailsMapper;

    @Autowired
    private CommodityMapper commodityMapper;

    @Override
    public int insertDetails() {
        return 0;
    }

    @Override
    public int checkCommodity(OrderDetails orderDetails) {
        //获得订单的ID
        Integer orderId = orderDetails.getOrderId();
        //根据订单的id查询出一个订单的订单详情
        List<OrderDetails> detailsList = ordersDetailsMapper.findByID(orderId);
        for (OrderDetails orderDatil:detailsList) {
            Integer commodityId = orderDatil.getCommodityId();
            Commodity commodity=commodityMapper.findById(commodityId);
            if (commodity.getState()!=0){
                Result.fail("商品的状态不对");
                log.info("商品的状态不对");
                return 1;
            }else if (commodity.getIsdelete()!=0){
                Result.fail("商品不存在");
                log.info("商品不存在");
                return 2;
            }else if (commodity.getNumber()<orderDatil.getPrice()){
                Result.fail("商品数量不足");
                log.info("商品数量不足");
                return 3;
            }
        }
        return 0;
    }

    @Override
    public int confirmOrders(Integer id) {

        return ordersDetailsMapper.confirmOrders(id);
    }
}
